
package com.alex.web.service.impl;

import com.alex.web.bean.member.MpingtHyxx;
import com.alex.web.dao.MpingtHyxxDao;
import com.alex.web.db.domain.PageControl;
import com.alex.web.db.domain.PageInfo;
import com.alex.web.service.MpingtHyxxService;
import com.alex.common.CommonUtil;
//import com.alex.common.MD5Util;
//import com.alex.common.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 平台会员信息表
 */
@Service
public class MpingtHyxxServiceImpl implements MpingtHyxxService {

    @Autowired
    private MpingtHyxxDao mpingtHyxxDao;

	/**
	 * 判断是否已经存在手机号
	 * @param phone
	 * @return
	 */
    public MpingtHyxx getInfoByPhone(String phone){
		//判断手机号是否已经注册
		return mpingtHyxxDao.getEntityByObj(new MpingtHyxx (), "youxbz = 1 and dianh = '"+phone+"'");
	}
	/**
	 * 根据实体MpingtHyxx，新增平台会员信息表 信息
	 * 返回影响行数
	 */
    public Integer insertMpingtHyxx(MpingtHyxx mpingtHyxx, String captcha) {
    	//判断传入的MpingtHyxx是否为空
		if (mpingtHyxx == null) {
			return 0;
		}

		//判断手机号是否已经注册
		long k = mpingtHyxxDao.getCountByObj(mpingtHyxx, "youxbz = 1 and dianh = '"+mpingtHyxx.getDianh()+"'");
		if (k > 0){
			return 101;
		}

		//编号是否重复查询
		long c = mpingtHyxxDao.getCountByObj(mpingtHyxx, "youxbz = 1 and bianh = '"+mpingtHyxx.getBianh()+"'");
		if (c > 0){
			return 103;
		}
		String bianm = "";
		if (mpingtHyxx.getBianh() == null || mpingtHyxx.getBianh() == "") {
			int hashCodeV = CommonUtil.getUUID().hashCode();
			if (hashCodeV < 0) {    //有可能是负数
				hashCodeV = -hashCodeV;
			}
			//平台会员号使用UUID自动生成
			bianm = String.valueOf(hashCodeV);
			mpingtHyxx.setBianh(bianm);
		}
		if (mpingtHyxx.getMingc().trim().isEmpty()){
			mpingtHyxx.setMingc("TMW_" + mpingtHyxx.getBianh());
		}

		mpingtHyxx.setZhucsj(new Date());

		//手机短信验证(账号密码注册时，不需要短信验证)
		if(!captcha.trim().isEmpty()) {
			boolean ckCaptcha = true;//SMSUtil.getInstance().validateSmsCode(mpingtHyxx.getDianh(), captcha);
			if (!ckCaptcha) {
				return 102;
			}
		}
		//执行新增操作，具体请看 MpingtHyxxMapper.xml
		Integer i = mpingtHyxxDao.insert(mpingtHyxx);
		return i;
    }
	/**
	 * 根据实体MpingtHyxx，更新平台会员信息表 信息，用主键更新
	 * 返回影响行数
	 */
    public Integer updateMpingtHyxx(MpingtHyxx mpingtHyxx){
		//判断传入的MpingtHyxx是否为空
		if (mpingtHyxx == null) {
			return 0;
		}
//		long c = mpingtHyxxDao.getCountByObj(mpingtHyxx, "bianh = '"+mpingtHyxx.getBianh()+"'");
//		if (c > 0){
//			return -1;
//		}
		//执行更新操作，具体请看 MpingtHyxxMapper.xml
		Integer i = mpingtHyxxDao.update(mpingtHyxx);
		return i;
    }
	/**
	 * 分页查询
	 */
    public PageControl<MpingtHyxx> page(MpingtHyxx mpingtHyxx, PageInfo pageInfo, String whereSql, String orderSql){
		return mpingtHyxxDao.getPageByObj(mpingtHyxx, pageInfo, whereSql, orderSql);
    }
	/**
	 * 根据主键id，查询一个实体MpingtHyxx
	 * 返回实体MpingtHyxx
	 */
    public MpingtHyxx find(Integer id){
		return mpingtHyxxDao.getEntityById(id);
    }
	/**
	 * 根据主键id
	 * 返回影响行数
	 */
    public Integer delete(Integer id){
		//真正的删除了
		return mpingtHyxxDao.delete(id);
    }

	/**
	 * 作废恢复，有作废标志对有用
	 */
	public Integer zouf(Integer id,Integer bz){
		MpingtHyxx temp = new MpingtHyxx();
		//设置主键id
		temp.setId(id);
		if (bz>0){
			bz = 0;
		}else{
			bz = 1;
		}
		//置了MpingtHyxx的有效标志
		temp.setYouxbz(bz);
		//执行更新操作，具体请看 MpingtHyxxMapper.xml
		return mpingtHyxxDao.update(temp);
	}

	/**
	 * 获取所有有效的类型
	 * @return
	 */
	@Override
	public List<MpingtHyxx> getAllByYouxbz() {
		MpingtHyxx entity = new MpingtHyxx();
		return mpingtHyxxDao.getListByObj(entity," youxbz = 1 ");
	}

	/**
	 * 用户名密码
	 * @param username
	 * @param password
	 * @return
	 */
	@Override
	public MpingtHyxx ulogin(Integer index, String phone, String username, String password) {
		MpingtHyxx entity = new MpingtHyxx();
		String sql = "";
		if (index == 1){
//			entity.setDianh(phone);
			sql = "dianh = '"+phone+"' and youxbz = 1 ";
		}
		if (index == 2){
//			entity.setBianh(username);
//			entity.setMim(password);
//			password = MD5Util.getMisi32Code(password);
			sql = "bianh = '"+username+"' and mim = '"+password+"' and youxbz = 1 ";
		}
		return mpingtHyxxDao.getEntityByObj(entity, sql);
	}
	/**
	 * 根据手机号和密码验证会员
	 * @param phone
	 * @param openid
	 * @return
	 */
	@Override
	public MpingtHyxx getMember(String phone, String loginid, String openid){
		MpingtHyxx entity = new MpingtHyxx();
		if (!phone.trim().isEmpty() && phone != null && !phone.trim().equals("null")){
			entity.setDianh(phone);
		}
		if (!loginid.trim().isEmpty() && loginid != null && !loginid.trim().equals("null")) {
			entity.setBianh(loginid);
		}
		if (!openid.trim().isEmpty() && openid != null && !openid.trim().equals("null")) {
			entity.setWeixbh(openid);
		}
		return mpingtHyxxDao.getEntityByObj(entity, " youxbz = 1 ");
	}

	/**
	 * 根据微信用户Openid
	 * @param openid
	 * @return
	 */
	public MpingtHyxx getMemberByOpenid(String openid){
		MpingtHyxx entity = new MpingtHyxx();
		entity.setWeixbh(openid);
		return mpingtHyxxDao.getEntityByObj(entity, " youxbz = 1 ");
	}


}
